﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using Common.Util;

namespace DataCenter.Provider
{
    public class WarrantyMessageProvider : GenericProvider, IWarrantyMessageProvider
    {
        public bool Add(WarrantyMessage wSms)
        {
            try
            {
                repository.Add<WarrantyMessage>(wSms);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public List<WarrantyMessage> Search(string serial = null, string model = null, bool? transferFlag = null)
        {
            var querry = repository.GetQuery<WarrantyMessage>();
            if (!serial.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.SerialNo.ToUpper() == serial.ToUpper());
            }

            if (!model.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.Model.ToUpper().Contains(model.ToUpper()));
            }

            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (transferFlag.Value) querry = querry.Where(x => x.TransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.TransferFlag == null || x.TransferFlag.Length == 0 || x.TransferFlag.ToUpper() == flag);
            }

            return querry.ToList();
        }

        public WarrantyMessage Retrieve(string serial, string responseType)
        {
            var querry = repository.GetQuery<WarrantyMessage>();
            if (!serial.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.SerialNo.ToUpper() == serial.ToUpper());
            }

            if (!responseType.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.RespType.ToUpper().Equals(responseType.ToUpper()));
            }

            return querry.FirstOrDefault();
        }

        public bool Update(WarrantyMessage obj)
        {
            try
            {
                var updateRecord = Retrieve(obj.SerialNo, obj.RespType);
                if (updateRecord == null) return false;
                updateRecord.ConfirmUser = obj.ConfirmUser;
                updateRecord.CreateDate = obj.CreateDate;
                updateRecord.EndDate = obj.EndDate;
                updateRecord.EndUserCell = obj.EndUserCell;
                updateRecord.LastUpdateDate = obj.LastUpdateDate;
                updateRecord.Model = obj.Model;
                updateRecord.RespMsg = obj.RespMsg;
                updateRecord.RespType = obj.RespType;
                updateRecord.SellinDate = obj.SellinDate;
                updateRecord.SelloutDate = obj.SelloutDate;
                updateRecord.SerialNo = obj.SerialNo;
                updateRecord.SelloutTime = obj.SelloutTime;
                updateRecord.SmsYn = obj.SmsYn;
                updateRecord.StartDate = obj.StartDate;
                updateRecord.Suffix = obj.Suffix;
                updateRecord.TransferFlag = obj.TransferFlag;
                if (updateRecord.TransferFlag.IsYes()) updateRecord.TransferDate = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
